<!--
 * @Author: shenya
 * @Date: 2023-08-10 18:34:53
 * @Description:
-->
<template>
  <el-dialog
    :title="action=='add'?'新增问题':'编辑问题'"
    :visible.sync="visible"
    width="500px"
    append-to-body
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
    >
      <el-form-item
        label="问题描述"
        prop="questionDesc"
      >
        <el-input
          v-model="form.questionDesc"
          maxlength="20"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="排序"
        prop="sort"
      >
        <el-input v-model="form.sort"></el-input>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        :loading="loading"
        type="primary"
        @click="submitForm"
      >
        确 定
      </el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  insertSurvey,
  selectSurveyById,
  updateSurvey,
} from '@/api/core/follow';
export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    relateObject: {
      type: Object,
    },
    id: {
      type: String,
    },
    action: {
      type: String,
    },
  },
  data() {
    return {
      form: {},
      loading: false,
      rules: {
        questionDesc: [
          {
            required: true,
            message: '请输入问题描述',
            trigger: 'blur',
          },
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur',
          },
          {
            pattern: /^[0-9]*[1-9][0-9]*$/,
            message: '排序只能输入正整数',
          },
        ],
      },
    };
  },
  computed: {
    visible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit('input', val);
      },
    },
  },
  watch: {
    visible(val) {
      if (val) {
        if (this.action === 'edit') {
          this.getDetail();
        }
      } else {
        this.$refs['form'].resetFields();
      }
    },
  },
  methods: {
    getDetail() {
      selectSurveyById(this.id).then((res) => {
        this.form = res.data.data;
      });
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.action === 'add') {
            const params = {
              relatedId: this.relateObject.id,
              questionDesc: this.form.questionDesc,
              sort: this.form.sort,
            };
            insertSurvey(params)
              .then((res) => {
                if (!res.data) return;
                this.$emit('success');
                this.visible = false;
                this.loading = false;
                this.$message.success('新增成功！');
              })
              .catch((err) => {
                this.loading = false;
                throw new Error(err);
              });
          } else {
            const params = {
              id: this.id,
              questionDesc: this.form.questionDesc,
              sort: this.form.sort,
              relatedId: this.relateObject.id,
            };
            updateSurvey(params)
              .then((res) => {
                if (!res.data) return;
                this.$emit('success');
                this.visible = false;
                this.loading = false;
                this.$message.success('编辑成功！');
              })
              .catch((err) => {
                this.loading = false;
                throw new Error(err);
              });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
</style>
